home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / DDJMAG / DDJ9310.ZIP / 1993-OCT.ZIP / CPPMAN.ASC < prev    next >
Text File  |  1993-09-17  |  4KB  |  221 lines

  1. _C++MANIPULATORS AND APPLICATORS_
  2. by Reginald B. Charney
  3.  
  4. Example 1: 
  5.  
  6.  
  7. (a)
  8.  
  9. // quote argument string and then
  10. // output it.
  11. ostream& qStr(char* s)
  12. { return cout<<"'"<<s<<"'"; }
  13.  
  14. cout << "Output is a " <<
  15.   qStr("string") << ".";
  16.             
  17. (b) 
  18.  
  19. Output is a 'string'.
  20.  
  21. (c) 
  22. 'string'Output is a 0x48f2.
  23.  
  24.  
  25. Example 2: 
  26.  
  27. (a)
  28.  
  29. typedef ostream OS; // an abbrev
  30.  
  31. // quote argument string and then
  32. // output it.
  33. OS& aMF(OS& os,char* s)
  34. { return os << "'" << s << "'"; }
  35.  
  36. aMF(cout, "string");
  37.             
  38. (b)
  39.  
  40.  
  41. outputs on 
  42. cout the value:
  43.  
  44. 'string'
  45.  
  46.  
  47.  
  48. Example 3: 
  49.  
  50. typedef long (*FP)(int, int); 
  51.  
  52. // af is applicator function
  53. long af(FP f, int i, int j)
  54.   { return (*f)(i,j); }
  55.  
  56. long sum(int i, int j)
  57.   { return i+j; }
  58.  
  59. long dif(int i, int j)
  60.   { return i-j; }
  61.  
  62. af(sum,2,3); // returns (long)5
  63. af(dif,9,5); // returns (long)4
  64.  
  65.  
  66. Example 4: 
  67.  
  68. typedef ostream OS; // an abbrev
  69.  
  70. template<class T>
  71. class OMC // output manip class
  72. {
  73.   typedef OS& (*MF)(OS&, T);
  74.   MF mf;  // manip function
  75.   T  a;     // arg of type T
  76. public:
  77.   OMC(MF mmf,T aa)
  78.      : mf(mmf), a(aa) { }
  79.   friend OS& operator <<
  80.      (OS& os, const OMC<T>& mc)
  81.   { return (*mc.mf)(os,mc.a); }
  82. };
  83.  
  84.  
  85.  
  86. Example 5: 
  87.  
  88. (a)
  89.  
  90. // define manipulator interface
  91. // for manipulator function aMF.
  92.  
  93. OMC<char*> aMI(char* s)
  94. { return OMC<char*>(aMF,s); }
  95.  
  96. cout << "Value "<< aMI("string");
  97.  
  98. (b)
  99.  
  100. Value 'string'
  101.  
  102.  
  103.  
  104.  
  105. Example 6: 
  106.  
  107.  
  108. #include <iostream.h>
  109. typedef ostream OS; // an abbrev
  110.  
  111. // qSTR - manip function
  112.  
  113. OS& qSTR(OS& os,char* s)
  114. { return os << "'" << s << "'"; }
  115.  
  116. // OMC - Output Manipulator Class
  117. template<class T> class OMC {
  118.   typedef OS& (*MF)(OS&, T);
  119.   MF mf;  // manipulator fcn
  120.   T  a;     // arg of type T
  121. public:
  122.   OMC(MF mmf,T aa)
  123.      : mf(mmf), a(aa) { }
  124.   friend OS& operator <<
  125.      (OS& os, const OMC<T>& mc)
  126.   { return (*mc.mf)(os,omc.a); }
  127. };
  128.  
  129. // qStr - manip interface
  130. // for manip function qSTR
  131. OMC<char*> qStr(char* s)
  132. { return OMC<char*>(qSTR,s); }
  133.  
  134. // sample output expression
  135. cout << "Output is a " <<
  136.     qStr("string") << "\n";
  137.  
  138.  
  139. Example 7: 
  140.  
  141. #include <iostream.h>
  142. #include <iomanip.h>
  143.  
  144. typedef ostream OS; // an abbrev
  145. typedef char* CP;   // single token
  146.  
  147. // qSTR - manip function
  148. OS& qSTR(OS& os,CP s)
  149. { return os << "'" << s << "'"; }
  150.  
  151. // qStr - manip interface
  152. // for manip function qSTR
  153. OMANIP<CP> qStr(CP s)
  154. { return OMANIP<CP>(qSTR,s); }
  155.  
  156. // sample output expression
  157. cout << "Output is a "
  158.      << qStr("string") << "\n";
  159.  
  160.  
  161. Example 8: 
  162.  
  163. #include <iostream.h>
  164. #include <iomanip.h>
  165.  
  166. typedef ostream OS; // an abbrev
  167. typedef char* CP; // single token
  168.  
  169. IOMANIPdeclare(CP);
  170.  
  171. // qSTR - manip function
  172. OS& qSTR(OS& os,CP s)
  173. { return os << "'" << s << "'"; }
  174.  
  175. // qStr - manip interface
  176. // for manipulator fcn qSTR
  177. OMANIP(CP) qStr(CP s)
  178.  
  179. { return OMANIP(CP)(qSTR,s); }
  180.  
  181. // sample output expression
  182. cout << "Output is a "
  183.      << qStr("string") << "\n";
  184.  
  185.  
  186.  
  187. Example 9: 
  188.  
  189. typedef ostream OS; // an abbrev
  190.  
  191. template<class T> class OAC {
  192.  
  193.   typedef OS& (*MF)(OS&, T);
  194.   MF mf;
  195. public:
  196.   OAC(MF mmf) : mf(mmf) { }
  197.   OMC<T> operator()(T a)
  198.      { return OMC<T>(mf,a); }
  199. };
  200.  
  201.  
  202. Example 10: 
  203.  
  204. #include <iostream.h>
  205. #include <iomanip.h>
  206.  
  207. typedef ostream OS; // an abbrev
  208. typedef char* CP;   // single token
  209.  
  210. // qSTR - manip function
  211. OS& qSTR(OS& os,CP s)
  212. { return os << "'" << s << "'"; }
  213.  
  214. OAPP<CP> qStr = qSTR;
  215.  
  216. // sample output expression
  217. cout << "Output is a "
  218.      << qStr("string") << "\n";
  219.  
  220.  
  221.